﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.CustomerManage;
using Leadtools.Codecs;
using Leadtools;
using Leadtools.WinForms;

namespace MINERP.CustomerManage.OrderInfo
{
    public partial class FrmCheckOrderInfo : Form
    {
        //参数
        private BLL.CustomerManage.CheckOrderInfoBLL bll = new MINERP.BLL.CustomerManage.CheckOrderInfoBLL();
        //属性
        private static FrmCheckOrderInfo _instance;
        public static FrmCheckOrderInfo GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmCheckOrderInfo();
                }
                return _instance;
            }
            set
            {
                _instance = value;
            }
        }

        public FrmCheckOrderInfo()
        {
            InitializeComponent();
        }

        private void FrmCheckOrderInfo_Load(object sender, EventArgs e)
        {

        }

        public void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                SetOrderInfo();
                //if (browser.Items != null && browser.Items.Count > 0)
                //{
                //    SetBigImage(browser.Items[0].Tag.ToString());
                //}
                txtOrderProductCode.SelectAll();
                txtOrderProductCode.Focus();
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void browser_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                string picPath = browser.SelectedItems[0].Tag.ToString();
                SetBigImage(picPath);
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //按客户名模糊查询
        private void btnFind_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                FrmFindByCtmName find = new FrmFindByCtmName();
                find.Frm = this;
                find.ShowDialog();
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //--------------------------------公共方法-----------------------------------------------
        private void SetOrderInfo()
        {
            try
            {
                string code = txtOrderProductCode.Text.Trim();
                if (code.Length < 10)
                {
                    int c = 0;
                    try
                    {
                        c = Convert.ToInt32(code);
                    }
                    catch
                    {
                        MessageBox.Show("请输入正确的产品编号！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtOrderProductCode.SelectAll();
                        txtOrderProductCode.Focus();
                        return;
                    }
                    code = "2" + string.Format("{0:d9}", c);
                }
                List<BLL.CustomerManage.CheckOrderInfoBLL.CheckPhoto> photos = bll.GetPhotosByOrderProductCode(code);
                if (photos == null || photos.Count == 0)
                    return;
                lblOrderProductName.Text = photos[0].OrderProductName;
                lblOrderProductCode.Text = photos[0].OrderProductCode;
                lblCtmName.Text = photos[0].CtmName;
                lblShopName.Text = photos[0].ShopName;
                lblPhotoNum.Text = photos.Count.ToString();
                AddThumbs(photos);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void AddThumbs(List<CheckOrderInfoBLL.CheckPhoto> photos)
        {
            browser.SelectedIndexChanged -= browser_SelectedIndexChanged;
            try
            {
                browser.Items.Clear();
                RasterCodecs.Startup();
                RasterCodecs codecs = new RasterCodecs();
                CodecsThumbnailOptions option = new CodecsThumbnailOptions();
                option.LoadStamp = true;
                option.MaintainAspectRatio = true;
                option.Resample = true;
                RasterImage img;
                foreach (CheckOrderInfoBLL.CheckPhoto p in photos)
                {
                    img = codecs.ReadThumbnail(p.PicPath, option, 1);
                    RasterImageListItem item = new RasterImageListItem(img, 1, p.PhotoRemark);
                    item.Tag = p.PicPath;
                    browser.Items.Add(item);
                }
                RasterCodecs.Shutdown();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            browser.SelectedIndexChanged += browser_SelectedIndexChanged;
        }

        private void SetBigImage(string picPath)
        {
            try
            {
                lblPhotoName.Text = picPath.Substring(picPath.LastIndexOf("\\") + 1);
                RasterCodecs.Startup();
                RasterCodecs codecs = new RasterCodecs();
                RasterImage img = codecs.Load(picPath);
                rasterImg.SizeMode = RasterPaintSizeMode.FitAlways;
                rasterImg.Image = img;
                RasterCodecs.Shutdown();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
